package com.jerei.plugin.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jerei.plugin.api.IMenuDetailService;
import com.jerei.plugin.dao.IWcmCommonMenuDetailDao;
import com.jerei.plugin.entity.WcmCommonAdminuser;
import com.jerei.plugin.entity.WcmCommonMenuDetail;
import com.jerei.plugin.service.IWcmCommonMenuDetailService;
import com.jerei.utils.UtilsCommon;
import com.jerei.utils.condition.ConditionEntity;
import com.jerei.utils.spring.PageBean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ro.fortsoft.pf4j.Extension;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by sunfeihu on 2016/10/21.
 */
@Extension
@Service
public class WcmCommonMenuDetailServiceImpl implements IWcmCommonMenuDetailService, IMenuDetailService {

    private Logger logger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);

    @Autowired
    private IWcmCommonMenuDetailDao iWcmCommonMenuDetailDao;

    @Override
    public WcmCommonMenuDetail save(WcmCommonMenuDetail entity) {
        return iWcmCommonMenuDetailDao.save(entity);
    }

    @Override
    public int delete(Long id) {
        return iWcmCommonMenuDetailDao.delete(id);
    }

    @Override
    public int update(WcmCommonMenuDetail entity) {
        return iWcmCommonMenuDetailDao.update(entity);
    }

    @Override
    public WcmCommonMenuDetail find(Long id) {
        return iWcmCommonMenuDetailDao.find(id);
    }

    @Override
    public PageBean<WcmCommonMenuDetail> page(int nowPage, int pageSize, ConditionEntity conditionEntity) {
        return iWcmCommonMenuDetailDao.page(nowPage, pageSize, conditionEntity);
    }

    @Override
    public List<WcmCommonMenuDetail> getRootMenuDetail(WcmCommonAdminuser adminuser) {
        return iWcmCommonMenuDetailDao.getRootMenuDetail(adminuser);
    }

    @Override
    public List<WcmCommonMenuDetail> getLeftMenuDetail(Long adminuserId, Long rootId) {
        List<WcmCommonMenuDetail> leftMenuDetails = iWcmCommonMenuDetailDao.getMenuDetailsByParent(adminuserId, rootId);
        if (null != leftMenuDetails && leftMenuDetails.size() > 0) {
            List<WcmCommonMenuDetail> subs = null;
            for (WcmCommonMenuDetail wcmd : leftMenuDetails) {
                subs = iWcmCommonMenuDetailDao.getMenuDetailsByParent(adminuserId, wcmd.getId());
                wcmd.setSubs(subs);
            }
        }
        return leftMenuDetails;
    }

    @Override
    public void save(List<WcmCommonMenuDetail> list) {
        iWcmCommonMenuDetailDao.save(list);
    }

    @Override
    public void delete(List<Long> ids) {
        iWcmCommonMenuDetailDao.delete(ids);
    }

    @Override
    public void update(List<WcmCommonMenuDetail> list) {
        iWcmCommonMenuDetailDao.update(list);
    }


    @Override
    public PageBean<WcmCommonMenuDetail> devRootMenuDetail(int nowPage, int pageSize) {
        return iWcmCommonMenuDetailDao.pageDevRoot(nowPage, pageSize);
    }

    @Override
    public PageBean<WcmCommonMenuDetail> groupRootMenuDetail(int nowPage, int pageSize, Long gid) {
        return iWcmCommonMenuDetailDao.pageGroupRoot(nowPage, pageSize, gid);
    }

    @Override
    public JSONArray treeMenuDetail(Long pid) {
        JSONArray jsonArray = null;
        List<WcmCommonMenuDetail> children = iWcmCommonMenuDetailDao.getMenuDetailsByParentNoPower(pid);
        if (null != children && children.size() > 0) {
            JSONObject obj = null;
            JSONArray _children = null;
            jsonArray = new JSONArray();
            for (WcmCommonMenuDetail child : children) {
                obj = (JSONObject) JSON.toJSON(child);
                _children = this.treeMenuDetail(obj.getLong("id"));
                if (null != _children) {
                    obj.put("children", _children);
                }
                jsonArray.add(obj);
            }
        }
        return jsonArray;
    }

    @Override
    public Set<String> findPermissionsString(Long[] roleIds) {
        List<Map<String, Object>> list = iWcmCommonMenuDetailDao.findPermissions(roleIds);
        Set<String> permissionsSet = new HashSet<String>();
        if (null != list && list.size() > 0) {
            String[] permArr = null;
            String id = "";
            String pluginId = "";
            String permissions = "";
            String model = "";
            for (Map<String, Object> m : list) {
                permissions = UtilsCommon.getString(m.get("permissions"));
                id = UtilsCommon.getString(m.get("id"));
                pluginId = UtilsCommon.getString(m.get("plugin_id"));
                if (null == permissions && "".equals(permissions)) {
                    continue;
                }
                permArr = permissions.split(",");
                model = pluginId;
                for (String perm : permArr) {
                    permissionsSet.add(model + ":" + perm);
                }
            }
        }
        return permissionsSet;
    }
}
